package com.shchuangzhi.smartguard;

import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.shchuangzhi.smartguard.core.NotifyMessage;

import java.util.List;

/**
 * Created by j_hu on 5/11/17.
 */

public class NotifyMessageViewAdapter extends RecyclerView.Adapter {
    private List<NotifyMessage> list;

    public NotifyMessageViewAdapter(List<NotifyMessage> list) {
        this.list = list;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int type) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.list_event_message, null);
        //不知道为什么在xml设置的“android:layout_width="match_parent"”无效了，需要在这里重新设置
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        view.setLayoutParams(lp);
        return new NotifyMessageViewHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int i) {
        NotifyMessageViewHolder holder = (NotifyMessageViewHolder)viewHolder;
        NotifyMessage msg = list.get(i);
        holder.tvEventTime.setText(msg.getTime());
        holder.tvEventLevel.setText(msg.getLevel());
        if (msg.getLevel().equals(NotifyMessage.LEVEL_ERROR)) {
            holder.tvEventLevel.setTextColor(Color.RED);
        } else {
            holder.tvEventLevel.setTextColor(Color.GREEN);
        }
        holder.tvEventMessage.setText(msg.getMessage());
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public void reload(List<NotifyMessage> list) {
        this.list = list;
    }

    class NotifyMessageViewHolder extends RecyclerView.ViewHolder {
        public TextView tvEventTime;
        public TextView tvEventLevel;
        public TextView tvEventMessage;

        public NotifyMessageViewHolder(View itemView) {
            super(itemView);
            tvEventTime = (TextView) itemView.findViewById(R.id.tv_event_time);
            tvEventLevel = (TextView) itemView.findViewById(R.id.tv_event_level);
            tvEventMessage = (TextView) itemView.findViewById(R.id.tv_event_message);
        }
    }
}
